﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Management_PersonnelOfficer_WebUserControl : System.Web.UI.UserControl
{

    class Chucvu
    {
        public int ID;
        public String Name;
    }
    static List<Chucvu> listChucvu;

    class Donvi
    {
        public int ID;
        public String Name;
    }

    static int currentIDStaff;
    static List<Donvi> listDonvi;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            currentIDStaff = -1;
            liPosition.Items.Clear();
            liWard.Items.Clear();
            listChucvu = new List<Chucvu>();
            listDonvi = new List<Donvi>();
            SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
            connection.Open();
            SqlCommand cmd1 = new SqlCommand("SELECT * FROM tbPosition", connection);
            SqlDataReader reader1 = cmd1.ExecuteReader();
            while (reader1.Read())
            {
                Chucvu cvu = new Chucvu();
                cvu.ID = reader1.GetInt32(0);
                cvu.Name = reader1.GetString(1).Trim();
                listChucvu.Add(cvu);
            }
            reader1.Close();
            foreach (Chucvu c in listChucvu)
            {
                liPosition.Items.Add(c.Name);
                liEPosition.Items.Add(c.Name);
            }
            //======================//
            SqlCommand cmd2 = new SqlCommand("SELECT * FROM tbWard", connection);
            SqlDataReader reader2 = cmd2.ExecuteReader();
            while (reader2.Read())
            {
                Donvi dvi = new Donvi();
                dvi.ID = reader2.GetInt32(0);
                dvi.Name = reader2.GetString(1).Trim();
                listDonvi.Add(dvi);
            }
            reader2.Close();
            foreach (Donvi c in listDonvi)
            {
                liWard.Items.Add(c.Name);
            }

            connection.Close();
        }
    }
    //==============================//
    public int getCurrentIDPosition()
    {
        if (liPosition.SelectedItem != null)
            foreach (Chucvu cvu in listChucvu)
            {
                if (cvu.Name.Equals(liPosition.SelectedItem.ToString()))
                    return cvu.ID;
            }
        return 0;
    }

    public void setSelectedIndexPosition(int index)
    {
        liPosition.SelectedIndex = index;
    }

    public int getIDPositionByName(String Name)
    {
        foreach (Chucvu cvu in listChucvu)
        {
            if (cvu.Name.Equals(Name))
            {
                return cvu.ID;
            }
        }
        return 0;
    }

    public String getCurrentNamePosition()
    {
        return liPosition.SelectedValue;
    }
    //==================================//

    public int getCurrentIDWard()
    {
        if (liWard.SelectedItem != null)
            foreach (Donvi dvi in listDonvi)
            {
                if (dvi.Name.Equals(liWard.SelectedItem.ToString()))
                    return dvi.ID;
            }
        return 0;
    }

    public string getNamePosistion(int idPosition)
    {
        SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        connection.Open();
        String sql = "SELECT Name FROM tbPosition WHERE ID = "+idPosition;
        SqlCommand cmd = new SqlCommand(sql,connection);
        SqlDataReader reader = cmd.ExecuteReader();
        string name=null;
        while(reader.Read()){
        name = reader.GetString(0);
        }
        reader.Close();
        connection.Close();
        return name;
    }

    public string getNameWard(int idWard)
    {
        SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        connection.Open();
        String sql = "SELECT Name FROM tbWard WHERE ID = " + idWard;
        SqlCommand cmd = new SqlCommand(sql, connection);
        SqlDataReader reader = cmd.ExecuteReader();
        string name = null;
        while (reader.Read())
        {
            name = reader.GetString(0);
        }
        reader.Close();
        connection.Close();
        return name;
    }

    public string getNameSex(int idSex)
    {
        if (idSex == 0)
            return "Nam";
        else return "Nữ";

    }

    public void setSelectedIndexWard(int index)
    {
        liWard.SelectedIndex = index;
    }

    public int getIDWardByName(String Name)
    {
        foreach (Donvi dvi in listDonvi)
        {
            if (dvi.Name.Equals(Name))
            {
                return dvi.ID;
            }
        }
        return 0;
    }

    public string getCurrentNameWard()
    {
        return liWard.SelectedValue;
    }

    public void lockEdit(Boolean locked)
    {
        txtFirstName.ReadOnly = locked;
        txtLastName.ReadOnly = locked;
        txtAddress.ReadOnly = locked;
        liSex.Enabled = !locked;
        dpDOB.ReadOnly = locked;
        txtTel.ReadOnly = locked;
        txtInsNum.ReadOnly = locked;
        liPosition.Enabled = !locked;
        liWard.Enabled = !locked;
        txtCurSalary.ReadOnly = locked;
        txtSalaryScale.ReadOnly = locked;
        txtHourperWeek.ReadOnly = locked;
        liPaid.Enabled = !locked;
        liWork.Enabled = !locked;
        txtQType.ReadOnly = locked;
        dpQDate.ReadOnly = locked;
        txtQIns.ReadOnly = locked;
        liEPosition.Enabled = !locked;
        dpStart.ReadOnly = locked;
        dpFinish.ReadOnly = locked;
        txtEOrganization.ReadOnly = locked;
    }
    public string setDate(DateTime date)
    {
        return date.Day+"-"+date.Month+"-"+date.Year;
    }

    public void fillInfor(int IDStaff)
    {
        currentIDStaff = IDStaff;
        SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        connection.Open();
        String sql = "SELECT * FROM tbStaff WHERE ID = "+IDStaff;
        SqlCommand cmd = new SqlCommand(sql,connection);
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            txtFirstName.Text = reader.GetString(1).Trim();
            txtLastName.Text = reader.GetString(2).Trim();
            txtAddress.Text = reader.GetString(3).Trim();
            liSex.Text = getNameSex(reader.GetInt32(4));
            dpDOB.Text =  setDate(reader.GetDateTime(5));
            txtTel.Text = reader.GetString(6).Trim();
            txtInsNum.Text = reader.GetString(7).Trim();
            liPosition.Text = getNamePosistion(reader.GetInt32(8));
            liWard.Text = getNameWard(reader.GetInt32(9));
            txtCurSalary.Text = reader.GetDouble(10).ToString();
            txtSalaryScale.Text = reader.GetDouble(11).ToString();
            txtHourperWeek.Text = reader.GetDouble(12).ToString();
            liPaid.SelectedValue =  reader.GetString(13).Trim();
            liWork.SelectedValue = reader.GetString(14).Trim();
            txtQType.Text = reader.GetString(15).Trim();
            dpQDate.Text = setDate(reader.GetDateTime(16));
            txtQIns.Text = reader.GetString(17).Trim();
            liEPosition.Text = getNamePosistion(reader.GetInt32(18));
            dpStart.Text = setDate(reader.GetDateTime(19));
            dpFinish.Text = setDate(reader.GetDateTime(20));
            txtEOrganization.Text = reader.GetString(21).Trim();
        }
        reader.Close();
        connection.Close();
    }
    public void DeleteData()
    { 
      if(currentIDStaff != -1)
      {
          SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
          connection.Open();
          String sql = "DELETE FROM tbStaff WHERE ID = "+currentIDStaff;
          if(txtFirstName.Text=="")
          {
              Response.Write("<script> alert('Chọn nhân viên để xóa!'); </script>");
          }
          else{
          SqlCommand cmd = new SqlCommand(sql, connection);
          int check = cmd.ExecuteNonQuery();
          if (check != 0)
          {
              Response.Write("<script> alert('Xóa Nhân viên thành công!'); </script>");
              txtFirstName.Text = "";
              txtLastName.Text = "";
              txtAddress.Text = "";
              txtCurSalary.Text = "";
              txtEOrganization.Text = "";
              txtHourperWeek.Text = "";
              txtInsNum.Text = "";
              txtQIns.Text = "";
              txtQType.Text = "";
              txtSalaryScale.Text = "";
              txtTel.Text = "";
              dpDOB.Text = "";
              dpQDate.Text = "";
              dpStart.Text = "";
              dpFinish.Text = "";
          }
          else
          {
              Response.Write("<script> alert('Lỗi khi xóa Nhân viên'); </script>");
          }
      }
      }
    }
    public void UpdateData()
    {
        if (currentIDStaff != -1)
        {
            SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);
            connection.Open();
            String sql = "UPDATE tbStaff SET " +
                "FirstName=@p01, " +
                "LastName=@p02, " +
                "Address=@p03, " +
                "Sex=@p04, " +
                "DOB=@p05, " +
                "Tel=@p06, " +
                "InsurranceNumber=@p07, " +
                "IDPosition=@p08, " +
                "IDWard=@p09, " +
                "CurrentSalary=@p10, " +
                "SalaryScale=@p11, " +
                "HourPerWeek=@p12, " +
                "PaidMode=@p13, " +
                "WorkMode=@p14, " +
                "QType=@p15, " +
                "QDate=@p16, " +
                "QInstitution=@p17, " +
                "EIDPosition=@p18, " +
                "EStartDate=@p19, " +
                "EFinishDate=@p20, " +
                "EOrganization=@p21 " +
                "WHERE ID=" + currentIDStaff;
            SqlCommand cmd = new SqlCommand(sql,connection);
            cmd.Parameters.AddWithValue("p01",txtFirstName.Text);
            cmd.Parameters.AddWithValue("p02",txtLastName.Text);
            cmd.Parameters.AddWithValue("p03",txtAddress.Text);
            cmd.Parameters.AddWithValue("p04",liSex.SelectedIndex);
            cmd.Parameters.AddWithValue("p05",dpDOB.Date);
            cmd.Parameters.AddWithValue("p06",txtTel.Text);
            cmd.Parameters.AddWithValue("p07",txtInsNum.Text);
            cmd.Parameters.AddWithValue("p08",getIDPositionByName(liPosition.SelectedItem.Text));
            cmd.Parameters.AddWithValue("p09",getIDWardByName(liWard.SelectedItem.Text));
            cmd.Parameters.AddWithValue("p10",txtCurSalary.Text);
            cmd.Parameters.AddWithValue("p11",txtSalaryScale.Text);
            cmd.Parameters.AddWithValue("p12",txtHourperWeek.Text);
            cmd.Parameters.AddWithValue("p13",liPaid.SelectedValue);
            cmd.Parameters.AddWithValue("p14", liWork.SelectedValue);
            cmd.Parameters.AddWithValue("p15",txtQType.Text);
            cmd.Parameters.AddWithValue("p16",dpQDate.Date);
            cmd.Parameters.AddWithValue("p17",txtQIns.Text);
            cmd.Parameters.AddWithValue("p18",getIDPositionByName(liEPosition.SelectedItem.Text));
            cmd.Parameters.AddWithValue("p19",dpStart.Date);
            cmd.Parameters.AddWithValue("p20",dpFinish.Date);
            cmd.Parameters.AddWithValue("p21",txtEOrganization.Text);
            int check = cmd.ExecuteNonQuery();
            if (check != 0)
            {
                Response.Write("<script> alert('Chập nhật thông tin Nhân viên thành công!'); </script>");
            }
            else
            {
                Response.Write("<script> alert('Có lỗi phát sinh'); </script>");
            }
            connection.Close();
        }
    }

}